package com.eqdd.yiqidian.ui.gongneng.renshi.meeting;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.bigkoo.alertview.OnItemClickListener;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewRefreshCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.MeetingNoticeBean;
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.utils.TimeUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ChooseAlertView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import io.reactivex.Flowable;

/**
 * 与个人有关的会议通知
 * Created by jhj on 18-5-28.
 */

public class MeetingNoticeListActivity extends BaseActivity {
    private RecyclerViewRefreshCustom dataBinding;
    private SlimAdapterEx<MeetingNoticeBean> slimAdapterEx;
    private MVCCoolHelper<List<MeetingNoticeBean>> mvcHelper;
    private int pageNum = 0;
    private List<MeetingType> meetingType;
    private int type; //0-领导、人事  1-个人

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview_refresh);
        type = getIntent().getIntExtra(Config.TYPE, -1);
        if (type == -1) {
            finish();
            ToastUtil.showShort("获取会议通知失败");
            return;
        }
        initTopTitleBar(View.VISIBLE, "会议通知");
        getType();
        initTopRightText("新增", v -> {
            ArrayList<String> list = new ArrayList<>();
            if (meetingType == null || meetingType.size() == 0) {
                ToastUtil.showShort("没有可发布的会议类型");
                return;
            } else {
                for (MeetingType type : meetingType) {
                    list.add(type.getType());
                }
            }

            ChooseAlertView.getBottomChoose(this, null, list, new OnItemClickListener() {
                @Override
                public void onItemClick(Object o, int position) {
                    Bundle bundle = new Bundle();
                    bundle.putInt(Config.ID, meetingType.get(position - 1).getId());
                    startActivityForResult(MeetingNoticeAddActivity.class, bundle, Config.ADD);
                }
            }).show();

        });
    }

    private void getType() {
        OkGo.<HttpResult<List<MeetingType>>>post(HttpConfig.BASE_URL + HttpConfig.MEETING_NOTICE_TYPE_PERSONAL)
                .params("userGuid", user.getGuid())
                .params("companyId", user.getCompanyId())
                .params("page", 0)
                .execute(new JsonCallBack<HttpResult<List<MeetingType>>>() {
                    @Override
                    public void onSuccess(Response<HttpResult<List<MeetingType>>> response) {
                        HttpResult<List<MeetingType>> result = response.body();
                        if (result.getStatus() == 200) {
                            meetingType = result.getItems();
                        }
                    }
                });

    }

    @Override
    public void initData() {
        if (slimAdapterEx == null) {
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
            dataBinding.recyclerView.addItemDecoration(new DividerLineItemDecoration(this));
            slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_145, new SlimInjector<MeetingNoticeBean>() {
                @Override
                public void onInject(MeetingNoticeBean data, IViewInjector injector) {
                    injector.text(R.id.tv_type, data.getType())
                            .text(R.id.tv_time, String.format(Locale.CHINA, "%s 到 %s",
                                    TimeUtil.formatWebTime(data.getStartTime()), TimeUtil.formatWebTime(data.getEndTime())))
                            .text(R.id.tv_place, data.getPlace());
                }
            }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
            ItemClickSupport.addTo(dataBinding.recyclerView).setOnItemClickListener((recyclerView, position, v) -> {
                int id = slimAdapterEx.getDataItem(position).getId();
                Bundle bundle = new Bundle();
                bundle.putInt(Config.ID, id);
                startActivityForResult(MeetingNoticeInfoActivity.class, bundle, Config.UPDATE);
            });


            String url;
            if (type == 0) {
                url = HttpConfig.BASE_URL + HttpConfig.MEETING_NOTICE_ALL;
            } else if (type == 1) {
                url = HttpConfig.BASE_URL + HttpConfig.MEETING_NOTICE_PERSONAL;
            } else {
                return;
            }
            ModelRx2DataSource<MeetingNoticeBean> dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
                @Override
                public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                    pageNum = page == 1 ? 0 : pageNum;
                    return OkGo.<HttpResult<List<MeetingNoticeBean>>>post(url)
                            .params("userGuid", user.getGuid())
                            .params("comid", user.getCompanyId())
                            .params("page", pageNum)
                            .converter(new JsonConverter<HttpResult<List<MeetingNoticeBean>>>() {
                                @Override
                                public void test() {
                                    super.test();
                                }
                            }).adapt(new FlowableBody<>())
                            .flatMap(result -> {
                                if (page == 1) {
                                    mvcHelper.setAutoLoadMore(true);
                                }
                                pageNum = result.getNextpage();
                                return Flowable.just(result.getItems() == null ? new ArrayList() : result.getItems());
                            });
                }
            }, 10);
            mvcHelper = new MVCCoolHelper<>(dataBinding.coolRefreshView);
            mvcHelper.setDataSource(dataSource);
            mvcHelper.setAdapter(slimAdapterEx);
            mvcHelper.refresh();
        }
    }


    @Override
    public void setView() {


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == Config.UPDATE) {
                mvcHelper.refresh();
            } else if (requestCode == Config.ADD) {
                mvcHelper.refresh();
            }
        }

    }

    class MeetingType {
        private int Id;
        private String type;

        public int getId() {
            return Id;
        }

        public void setId(int id) {
            this.Id = id;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }
    }
}
